package com.ppc.ugo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ppc.ugo.dao.mapper.CategoryMapper;
import com.ppc.ugo.dao.pojo.Category;
import com.ppc.ugo.service.CategoryService;
import com.ppc.ugo.vo.CategoryVo;
import com.ppc.ugo.vo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
@Slf4j
public class CategoryServiceImpl implements CategoryService {
    @Resource
    private CategoryMapper categoryMapper;

    @Override
    public void addCategory(Category category) {
        categoryMapper.insert(category);

        log.info("成功添加种类：{}", category.getCat_name());
    }

    @Override
    public Result getCategories() {
        List<CategoryVo> resultList = new ArrayList<>();



        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        int level = 0;
        queryWrapper.eq(Category::getCat_level, level);
        List<Category> categories = categoryMapper.selectList(queryWrapper);            //0级
        for (Category category : categories) {
            queryWrapper.clear();
            queryWrapper.eq(Category::getCat_pid, category.getCat_id());
            CategoryVo l0item = new CategoryVo();
            BeanUtils.copyProperties(category, l0item);

            List<CategoryVo> l1List = new ArrayList<>();
            List<Category> l0children = categoryMapper.selectList(queryWrapper);        //1级
            for (Category l0child : l0children) {
                queryWrapper.clear();
                queryWrapper.eq(Category::getCat_pid, l0child.getCat_id());
                CategoryVo l1item = new CategoryVo();
                BeanUtils.copyProperties(l0child, l1item);

                List<CategoryVo> l2List = new ArrayList<>();
                List<Category> l1children = categoryMapper.selectList(queryWrapper);       //2级
                for (Category l1child : l1children) {
                    CategoryVo l2item = new CategoryVo();
                    BeanUtils.copyProperties(l1child, l2item);
                    l2List.add(l2item);
                }
                l1item.setChildren(l2List);
                l1List.add(l1item);
            }
            l0item.setChildren(l1List);
            resultList.add(l0item);
        }

        return Result.success(resultList);
    }

    @Override
    public List<Category> selectByLevel(Integer level) {
        List<Category> categories = categoryMapper.selectList(
                new LambdaQueryWrapper<Category>().eq(Category::getCat_level, level));
        return categories;
    }

    @Override
    public List<Category> selectByPid(Integer pid) {
        List<Category> categories = categoryMapper.selectList(
                new LambdaQueryWrapper<Category>().eq(Category::getCat_pid, pid));
        return categories;
    }
}
